capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/* 
Figure B11
*/

local data "" ;
local output "" ;

local genr_first "g" ;
local genr_title "income-gen" ;
local genr_xelabel "Crime" ;
local genr_xilabel "More crime" ;
local genr_nelabel "No crime" ;
local genr_nilabel "Less crime" ;

local e_title ">0" ;
local i_title ">1" ;

local c_title  ", 1997-2017" ;

local mult_age18unfav= 1 ;

local xlab_age18unfav "-.2(.1).2" ;

local margin_i "t" ;

foreach sample in all female male {;
foreach time in c { ;
	forval amt = 15(5)15 { ;
		foreach type in genr { ;
				foreach endo in age18unfav { ;
		
				use "prwora_cXe_bw264_crime_poly1allcov_iv_`sample'.dta", clear ;
				pause ;
				
				duplicates drop variables, force ;
			
				keep if 
					(variables == "n_`time'rime2emp`amt'``type'_first'_1997_2017" |
					 variables == "n_`time'rime2noemp`amt'``type'_first'_1997_2017" |
					 variables == "n_`time'rimeno2emp`amt'``type'_first'_1997_2017" |
					 variables == "n_`time'rimeno2noe`amt'``type'_first'_1997_2017" |
					 variables == "n_`time'rime2emp`amt'``type'_first't_1997_2017" |
					 variables == "n_`time'rime2noemp`amt'``type'_first't_1997_2017" |
					 variables == "n_`time'rimeno2emp`amt'``type'_first't_1997_2017" |
					 variables == "n_`time'rimeno2noe`amt'``type'_first't_1997_2017"
					 )
					;
					
				replace variables = "cnoXeyes_e" 	if variables == "n_`time'rimeno2emp`amt'``type'_first'_1997_2017" ;
				replace variables = "cnoXeno_e"  	if variables == "n_`time'rimeno2noe`amt'``type'_first'_1997_2017" ;
				replace variables = "cyesXeyes_e" if variables == "n_`time'rime2emp`amt'``type'_first'_1997_2017" ;
				replace variables = "cyesXeno_e" 	if variables == "n_`time'rime2noemp`amt'``type'_first'_1997_2017" ;
				
				replace variables = "cnoXeyes_i" 	if variables == "n_`time'rimeno2emp`amt'``type'_first't_1997_2017" ;
				replace variables = "cnoXeno_i"  	if variables == "n_`time'rimeno2noe`amt'``type'_first't_1997_2017" ;
				replace variables = "cyesXeyes_i" if variables == "n_`time'rime2emp`amt'``type'_first't_1997_2017" ;
				replace variables = "cyesXeno_i" 	if variables == "n_`time'rime2noemp`amt'``type'_first't_1997_2017" ;

				gen b_endo = n_`endo' * `mult_`endo'' ;
				rename sen_`endo' se_endo ;
				keep variables b_endo se_endo mean_rd ;
				gen lo95 = b_endo - 1.96*se_endo ;
				gen hi95 = b_endo + 1.96*se_endo ;
				
				gen treat_lvl = mean_rd + b_endo ;

				gen lo95_treat_lvl = treat_lvl - 1.96*se_endo ;
				gen hi95_treat_lvl = treat_lvl + 1.96*se_endo ;
				
				tempfile `sample'_`time'_`amt'_`type'_`endo' ;
				save ``sample'_`time'_`amt'_`type'_`endo'' ;

				} ; /* end endo */
		} ; /* end type */
	} ; /* end amt */
} ; /* end time */
} ; /* end sample */


local text_e "text(5.6 .29 "All", size(vsmall)) text(4.4 .24 "Female", size(vsmall)) text(4.6 .42 "Male", size(vsmall))" ;
local text_i "text(5.6 .28 "All", size(vsmall)) text(4.4 .22 "Female", size(vsmall)) text(4.6 .35 "Male", size(vsmall))" ;
local arrow_e "(pcarrowi 5.3 .27  5.5 .28  "", 	mstyle(none) lwidth(vthin) color(black)) (pcarrowi 4.7 .20  4.5 .21  "", 	mstyle(none) lwidth(vthin) color(black))(pcarrowi 4.9 .39  4.7 .41  "", 	mstyle(none) lwidth(vthin) color(black))" ;
local arrow_i "(pcarrowi 5.3 .25  5.5 .27  "", 	mstyle(none) lwidth(vthin) color(black)) (pcarrowi 4.7 .20  4.5 .21  "", 	mstyle(none) lwidth(vthin) color(black))(pcarrowi 4.9 .31  4.7 .33  "", 	mstyle(none) lwidth(vthin) color(black))" ;

foreach time in c { ;
	forval amt = 15(5)15 { ;
		foreach type in genr { ;
			foreach margin in e { ;
				foreach endo in age18unfav { ;

					use `all_`time'_`amt'_`type'_`endo'', clear ;
					gen sample = "all" ;
					append using `male_`time'_`amt'_`type'_`endo'' ;
					replace sample = "male" if sample == "" ; 
					append using `female_`time'_`amt'_`type'_`endo'' ;
					replace sample = "female" if sample == "" ;

					local all_add = .2 ;
					local male_add = 0 ;
					local female_add = -.2 ;

					gen sort = . ;
					foreach sample in all male female { ;
						replace	sort = 5 + ``sample'_add' 	if variables == "cyesXeno_`margin'" 	& sample == "`sample'" ;
						replace sort = 4 + ``sample'_add' 	if variables == "cyesXeyes_`margin'" 	& sample == "`sample'" ;
						replace sort = 3 + ``sample'_add' 	if variables == "cnoXeyes_`margin'" 	& sample == "`sample'"  ;
						replace sort = 2 + ``sample'_add'	if variables == "cnoXeno_`margin'" 	& sample == "`sample'"   ;
					} ;

					twoway 
									 
						/* Actual figure */
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cyesXeno_`margin'" 	& sample == "all", color(gray*.25) barwidth(.1) horizontal)
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cyesXeyes_`margin'" 	& sample == "all", color(gray*.25) barwidth(.1) horizontal)
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cnoXeno_`margin'" 	& sample == "all", color(gray*.25) barwidth(.1) horizontal)
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cnoXeyes_`margin'" 	& sample == "all", color(gray*.25) barwidth(.1) horizontal)
						
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cyesXeno_`margin'" 	& sample == "male", color(gray*.25) barwidth(.1) horizontal)
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cyesXeyes_`margin'" 	& sample == "male", color(gray*.25) barwidth(.1) horizontal)
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cnoXeno_`margin'" 	& sample == "male", color(gray*.25) barwidth(.1) horizontal)
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cnoXeyes_`margin'" 	& sample == "male", color(gray*.25) barwidth(.1) horizontal)
						
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cyesXeno_`margin'" 	& sample == "female", color(gray*.25) barwidth(.1) horizontal)
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cyesXeyes_`margin'" 	& sample == "female", color(gray*.25) barwidth(.1) horizontal)
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cnoXeno_`margin'" 	& sample == "female", color(gray*.25) barwidth(.1) horizontal)
						(rbar hi95_treat_lvl lo95_treat_lvl	sort 	if variables == "cnoXeyes_`margin'" 	& sample == "female", color(gray*.25) barwidth(.1) horizontal)
						
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cyesXeno_`margin'" 	& sample == "all", color(red*1.5) lwidth(medthick))
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cyesXeyes_`margin'" 	& sample == "all", color(red*.75) lwidth(medthick))
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cnoXeno_`margin'" 	& sample == "all", color(green*.75) lwidth(medthick))
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cnoXeyes_`margin'" 	& sample == "all", color(green*1.5) lwidth(medthick))
						
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cyesXeno_`margin'" 	& sample == "male", color(red*1.5) lwidth(medthin) lpattern(shortdash) msize(small))
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cyesXeyes_`margin'" 	& sample == "male", color(red*.75) lwidth(medthin) lpattern(shortdash) msize(small))
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cnoXeno_`margin'" 	& sample == "male", color(green*.75) lwidth(medthin) lpattern(shortdash) msize(small))
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cnoXeyes_`margin'" 	& sample == "male", color(green*1.5) lwidth(medthin) lpattern(shortdash) msize(small))
						
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cyesXeno_`margin'" 	& sample == "female", color(red*1.5) lwidth(medthin) lpattern(dash))
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cyesXeyes_`margin'" 	& sample == "female", color(red*.75) lwidth(medthin) lpattern(dash))
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cnoXeno_`margin'" 	& sample == "female", color(green*.75) lwidth(medthin) lpattern(dash))
						(pcarrow sort mean_rd sort treat_lvl 	if variables == "cnoXeyes_`margin'" 	& sample == "female", color(green*1.5) lwidth(medthin) lpattern(dash))

						(scatter sort mean_rd  	if variables == "cyesXeno_`margin'" 	& sample == "all", color(red*1.5) msymbol(X))
						(scatter sort mean_rd  	if variables == "cyesXeyes_`margin'" 	& sample == "all", color(red*.75) msymbol(D) msize(small))
						(scatter sort mean_rd  	if variables == "cnoXeno_`margin'" 	& sample == "all", color(green*.75) msymbol(O) msize(small))
						(scatter sort mean_rd  	if variables == "cnoXeyes_`margin'" 	& sample == "all", color(green*1.5) msymbol(T) msize(small))
						
						(scatter sort mean_rd  	if variables == "cyesXeno_`margin'" 	& sample == "male", color(red*1.5) msymbol(x))
						(scatter sort mean_rd  	if variables == "cyesXeyes_`margin'" 	& sample == "male", color(red*.75) msymbol(d) msize(small))
						(scatter sort mean_rd  	if variables == "cnoXeno_`margin'" 	& sample == "male", color(green*.75) msymbol(o) msize(small))
						(scatter sort mean_rd  	if variables == "cnoXeyes_`margin'" 	& sample == "male", color(green*1.5) msymbol(t) msize(small))
						
						(scatter sort mean_rd  	if variables == "cyesXeno_`margin'" 	& sample == "female", color(red*1.5) msymbol(x))
						(scatter sort mean_rd  	if variables == "cyesXeyes_`margin'" 	& sample == "female", color(red*.75) msymbol(d) msize(small))
						(scatter sort mean_rd  	if variables == "cnoXeno_`margin'" 	& sample == "female", color(green*.75) msymbol(o) msize(small))
						(scatter sort mean_rd  	if variables == "cnoXeyes_`margin'" 	& sample == "female", color(green*1.5) msymbol(t) msize(small))
						
						`arrow_`margin''					
						, 
						legend(off)
						title("Crime (``type'_title', ``margin'_title') and employment (>$15K, 50% yrs)``time'_title'", size(medsmall))
						ysc(r(1 6)) 
						ylabel(5 `" "``type'_xelabel'," "no work" "' 4 `" "``type'_xelabel'," "work" "' 
							   3 `" "``type'_nelabel'," "work" "' 2 `" "``type'_nelabel'," "no work" "', 
							   angle(0) notick labsize(small)) 
						ytitle("",) 
						xsc(r(-.05 1)) xlab(0(.2)1) xtitle("Mean (marker) + IV estimate (arrow)", size(medsmall))
						graphregion(color(white))
						bgcolor(white)
						ylabel(,nogrid angle(0))
						xline(0,lcolor(gray*1.5) lpattern(dot))
						plotregion(margin(0 2 2 2))
						`text_`margin''
						
						;
						graph export "`output'/figB11.png", width(1000) height(750) replace ;
			} ;
		} ;
	} ;
} ;
} ;
